package com.xlh.virtualization.controller;

import com.xlh.common.Result;
import com.xlh.exception.common.BadRequestException;
import com.xlh.security.UserInfoHolder;
import com.xlh.virtualization.po.ClipboardPO;
import com.xlh.virtualization.po.ResolutionAdjustPO;
import com.xlh.virtualization.service.ExperimentContainerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;

/**
 * @author cheer
 */
@Api("容器操作")
@RestController
@RequestMapping("/experiment/container")
public class ExperimentContainerController {

    @Autowired
    private UserInfoHolder userInfoHolder;

    @Autowired
    private ExperimentContainerService experimentContainerService;

    /*-------   下面4个接口用于 实验页面右侧工具栏   -------*/

    @ApiOperation("批量调整分辨率")
    @PostMapping("/display")
    // todo 需要传任务id
    public Result adjustResolution(@RequestBody @Valid ResolutionAdjustPO resolutionAdjustPO) {
        experimentContainerService.adjustResolution(resolutionAdjustPO);
        return Result.success();
    }

    @ApiOperation("查询虚拟机的ftp地址")
    @GetMapping("/{containerId}/ftp")
    public Result<String> queryFtpAddress(@PathVariable("containerId") Long containerId) {
        String ftpUrl = experimentContainerService.getFtpAddress(containerId);
        return Result.success(ftpUrl);
    }

    @ApiOperation("查询虚拟机的vnc地址")
    @GetMapping("/{containerId}/vnc")
    public Result<String> queryVncAddress(@PathVariable("containerId") Long containerId) {
        String vncUrl = experimentContainerService.getVncAddress(containerId);
        return Result.success(vncUrl);
    }

    @ApiOperation("文件上传")
    @PostMapping("/{containerId}/upload")
    public Result uploadFile(@PathVariable("containerId") Long containerId,
                             MultipartFile file) {
        if (file == null) {
            throw new BadRequestException("上传文件不能为空");
        }
        experimentContainerService.uploadFile(containerId, file);
        return Result.success();
    }

    @ApiOperation("剪切板")
    @PostMapping("/{containerId}/clipboard")
    // todo 需要传任务id
    public Result clipboard(@PathVariable("containerId") Long containerId,
                            @RequestBody @Valid ClipboardPO clipboardPO) {
        if (experimentContainerService.clipboard(containerId, clipboardPO, userInfoHolder.isStudent())) {
            return Result.success();
        }
        return Result.error("复制内容异常");
    }

    /*-------   end   -------*/
}
